package cn.ghf.pet.controller;

import cn.ghf.basic.exception.MyException;
import cn.ghf.basic.util.AjaxResult;
import cn.ghf.basic.util.LoginContext;
import cn.ghf.basic.util.PageList;
import cn.ghf.pet.domain.Pet;
import cn.ghf.pet.domain.SearchMasterMsg;
import cn.ghf.pet.query.SearchMasterMsgQuery;
import cn.ghf.pet.service.ISearchMasterMsgService;
import cn.ghf.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.loadAll();
    }

    /**
     * 宠物寻主信息发布
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            System.out.println(searchMasterMsg);
            //获取登录信息
            LoginInfo loginInfo = LoginContext.getLogin(request);
            System.out.println(loginInfo);
            searchMasterMsgService.publish(searchMasterMsg,loginInfo.getId());
            return AjaxResult.createAjaxResult();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }
    /**
     * 待处理消息 平台管理员或店铺管理员
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {

        //获取登录信息
        LoginInfo loginInfo = LoginContext.getLogin(request);

        //根据条件和登录用户id查询待处理消息
        return searchMasterMsgService.pending(query,loginInfo.getId());
    }

    /**
     * 已处理消息
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        //获取登录信息
        LoginInfo loginInfo = LoginContext.getLogin(request);
        //根据条件和登录用户id查询已处理消息
        return searchMasterMsgService.processed(query,loginInfo.getId());
    }

    /**
     * 宠物待处理消息处理功能
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLogin(request);
            searchMasterMsgService.handle(pet,loginInfo.getId());
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统异常");
        }
    }
}
